package Bidostar_Page;

import Bidostar_BaseData.Page;
import Bidostar_Data.Baseinfo;
import Bidostar_Data.Bidostar_GetCode;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class PhoneReportPage extends Page
{
    public PhoneReportPage(WebDriver driver)
    {
        super(driver);
    }

    //点击电话报案
    public void clickphonereport()
    {
        getElement("phonereport").click();
    }

    //点击添加电话报案
    public void clickaddphonereport()
    {
        getElement("addphonereport").click();
    }

    //点击选择本方车地区
    public void clickAddressSelect()
    {
        getElement("AddressSelect").click();
    }

    //选择青海地区
    public void clickaddress_qing()
    {
        getElement("address_Qing").click();
    }

    //输入本方车号码牌
    public void Input_BC(String Input_BC)
    {
        getElement("Input_BC").clear();
        getElement("Input_BC").sendKeys(Input_BC);
    }

    //添加关联车辆
    public void AddCar_Info()
    {
        getElement("AddCar_Info").click();
    }

    //他车地区选择
    public void OtherCarSelect()
    {
        getElement("OtherCarSelect").click();
    }

    //输入他车号码牌
    public void OtherCarInput(String OtherCarInput)
    {
        getElement("OtherCarInput").clear();
        getElement("OtherCarInput").sendKeys(OtherCarInput);
    }

    //取消他车选择
    public void clickDisOtherCar()
    {
        getElement("DisOtherCar").click();
    }

    //侦测车辆信息
    public void SenseReport()
    {
        getElement("SenseReport").click();
    }

    //如侦测不到车辆时点击弹窗中确定按钮
    public void clickAlert_Sure()
    {
        getElement("Alert_Sure").click();
    }

    //电话报案添加页面的姓名
    public void ReporName(String ReporName)
    {
        getElement("ReporName").clear();
        getElement("ReporName").sendKeys(ReporName);
    }

    //电话报案添加页面的手机号码
    public void phone(String phone)
    {
        getElement("phone").clear();
        getElement("phone").sendKeys(phone);
    }

    //电话报案添加页面的身份证号码
    public void driverLience(String driverLience)
    {
        getElement("driverLience").clear();
        getElement("driverLience").sendKeys(driverLience);
    }

    //电话报案页面事故类型选择
    public void SgType()
    {
        getElement("SgType").click();
    }

    //电话报案页面事故类型选择：追尾
    public void SgType_ZW()
    {
        getElement("SgType_ZW").click();
    }

    //电话报案添加页面的时间选择
    public void AddReportData(String AddReportData)
    {
        getElement("AddReportData").sendKeys(AddReportData);
    }

    //电话报案添加页面地址选择
    public void ReportAddress(String ReportAddress)
    {
        getElement("ReportAddress").clear();
        getElement("ReportAddress").sendKeys(ReportAddress);
    }

    //电话报案页面事故描述（最多100个字符）
    public void clickReportRemark(String ReportRemark)
    {
        getElement("ReportRemark").clear();
        getElement("ReportRemark").sendKeys(ReportRemark);
    }

    //电话报案页面确定添加按钮
    public void AddReportSure()
    {
        getElement("AddReportSure").click();
    }

    //电话报案单车选择
    public boolean AddPhoneReport_ByOneCar() throws Exception
    {
        if (driver.getCurrentUrl().contains("ci.bidostar.com") || driver.getCurrentUrl().contains("ta.bidostar.com"))
        {
            logger.warn("保险&交警平台暂不支持电话报案");
        } else if (driver.getCurrentUrl().contains("dc.bidostar.com"))
        {
            clickphonereport();
            clickaddphonereport();
            clickAddressSelect();
            clickaddress_qing();
            Input_BC(Baseinfo.PhoneReport_BC);
            if (Baseinfo.PhoneReport_BC.replaceAll(" +", "").length() == 6 && Baseinfo.checkIsLitter())
            {
                logger.info("车牌号输入正确：" + Baseinfo.PhoneReport_BC);
            } else
            {
                logger.warn("车牌号不合法,请重新输入");
            }
            SenseReport();
            if (Bidostar_GetCode.Device_Code() == null)
            {
                logger.info("当前车牌所对应账户为绑定设备");
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                logger.warn(getElement("FeedBack_Alert").getText());
                clickAlert_Sure();
            } else
            {
                logger.info("当前车牌对应账户所绑定设备Imei号为：" + Bidostar_GetCode.PhoneReport_Imei());
            }
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            if (getElement("ReporName").getAttribute("value").contains(Bidostar_GetCode.name()))
            {
                logger.info(getElement("ReporName").getAttribute("placeholder") + ":" + getElement("ReporName").getAttribute("value"));
            } else
            {
                ReporName(Baseinfo.ReportName());
            }
            if (getElement("phone").getAttribute("value").contains(Bidostar_GetCode.phone()))
            {
                logger.info(getElement("phone").getAttribute("placeholder") + ":" + getElement("phone").getAttribute("value"));
            } else
            {
                phone(Baseinfo.AccountTelephone());
            }
            if (getElement("driverLience").getAttribute("value").contains(Bidostar_GetCode.license_no()))
            {
                logger.info(getElement("driverLience").getAttribute("placeholder") + ":" + getElement("driverLience").getAttribute("value"));
            } else
            {
                driverLience(Baseinfo.PhoneReportDriverLience);
                if (Baseinfo.PhoneReportDriverLience.length() <= 0)
                {
                    logger.warn("身份证号码有误");
                }
            }
            SgType();
            SgType_ZW();
            if (getElement("ReportAddress").getAttribute("value").length() == 0)
            {
                ReportAddress("北京市海淀区海淀北二街8号");
            } else
            {
                logger.info(getElement("ReportAddress").getAttribute("placeholder") + ":" + getElement("ReportAddress").getAttribute("value"));
            }
            clickReportRemark(Baseinfo.Remark());
            AddReportSure();


            waitForWebElement(By.xpath("html/body/div[1]/div/div[2]/div[1]/div/p[1]"));

            if (driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div[1]/div/p[1]")).getText().contains(Bidostar_GetCode.ACL_id()))
            {
                logger.info("电话报案添加成功，案件ID为：" + Bidostar_GetCode.ACL_id());
                String date_Time = Bidostar_GetCode.ACL_Time();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                Date d;

                DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                try
                {
                    d = df.parse(date_Time);
                    String str = df2.format(d);
                    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                    if (driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div[2]/h6")).getText().contains(str))
                    {
                        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                        logger.info("报案时间为：" + driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div[2]/h6")).getText());
                    } else if (str.replaceAll(" +", "").length() < 0)
                    {
                        logger.error("未获取当前报案时间");
                    } else if (str == null)
                    {
                        logger.error("未获取当前报案时间");
                    } else
                    {
                        logger.error("SQL语句执行异常");
                    }
                } catch (Exception e)
                {
                    logger.info("异常");
                    ScreenGet();
                }
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            } else if (driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div[1]/div/p[1]")).getText().contains((Bidostar_GetCode.ACL_id2())))
            {
                logger.info("电话报案添加成功，案件ID为：" + Bidostar_GetCode.ACL_id2());
            } else
            {
                logger.warn("失败");
            }
        }
        return false;
    }
}